<template>
  <div class="register-card">
    <div class="register-header">
      <h2>创建账号</h2>
      <p>加入我们的社区，探索更多机会</p>
    </div>
    
    <!-- 注册表单 -->
    <form @submit.prevent="handleRegister" class="register-form">
      <div class="form-group">
        <label for="username">
          <i class="fas fa-user"></i>
          用户名
        </label>
        <input 
          type="text" 
          id="username" 
          v-model="registerForm.username" 
          required 
          placeholder="请输入用户名"
          autocomplete="username"
        >
      </div>
      
      <div class="form-group">
        <label for="password">
          <i class="fas fa-lock"></i>
          密码
        </label>
        <input 
          type="password" 
          id="password" 
          v-model="registerForm.password" 
          required 
          placeholder="请输入密码"
          autocomplete="new-password"
        >
      </div>
      
      <div class="form-group">
        <label for="confirmPassword">
          <i class="fas fa-lock"></i>
          确认密码
        </label>
        <input 
          type="password" 
          id="confirmPassword" 
          v-model="confirmPassword" 
          required 
          placeholder="请再次输入密码"
          autocomplete="new-password"
        >
      </div>
      
      <div class="terms-agree">
        <label class="checkbox-label">
          <input type="checkbox" v-model="agreeTerms" required>
          <span>我已阅读并同意<a href="#" @click.prevent="showTerms">服务条款</a>和<a href="#" @click.prevent="showPrivacy">隐私政策</a></span>
        </label>
      </div>
      
      <button type="submit" :disabled="loading || !agreeTerms" class="register-button">
        <span v-if="!loading">注册</span>
        <span v-else class="loading-spinner"></span>
      </button>
      
      <div class="login-link">
        已有账号？ <router-link to="/auth/login">立即登录</router-link>
      </div>
    </form>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/user'
import { ElMessage } from 'element-plus'

// 路由实例
const router = useRouter()
// 用户状态管理
const userStore = useUserStore()

// 注册表单数据
const registerForm = reactive({
  username: '',
  password: ''
})

// 确认密码
const confirmPassword = ref('')

// 是否同意条款
const agreeTerms = ref(false)

// 加载状态
const loading = ref(false)

// 检查是否已登录，如果已登录则跳转到首页
onMounted(() => {
  if (userStore.isLoggedIn) {
    router.push('/profile')
  }
})

// 注册处理
const handleRegister = async () => {
  // 基本表单验证
  if (!registerForm.username.trim()) {
    ElMessage.warning('请输入用户名')
    return
  }
  
  // 密码验证
  if (!registerForm.password) {
    ElMessage.warning('请输入密码')
    return
  }
  
  if (registerForm.password.length < 6) {
    ElMessage.warning('密码长度至少为6个字符')
    return
  }
  
  // 两次密码一致性验证 (仅前端校验)
  if (registerForm.password !== confirmPassword.value) {
    ElMessage.error('两次输入的密码不一致')
    return
  }

  try {
    loading.value = true
    // 调用store中的注册方法，只传递用户名和密码
    const success = await userStore.register({
      username: registerForm.username,
      password: registerForm.password
    })

    if (success) {
      ElMessage.success('注册成功')
      // 注册成功后跳转到登录页
      setTimeout(() => {
        router.push('/auth/login')
      }, 1500)
    } else {
      ElMessage.error('注册失败，请检查输入信息')
    }
  } catch (error) {
    console.error('注册异常:', error)
    ElMessage.error(error.message || '注册失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 显示服务条款
const showTerms = () => {
  ElMessage.info('服务条款功能开发中...')
}

// 显示隐私政策
const showPrivacy = () => {
  ElMessage.info('隐私政策功能开发中...')
}
</script>

<style scoped>
.register-card {
  width: 100%;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  padding: 2.5rem;
  transition: all 0.3s ease;
}

.register-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15);
}

.register-header {
  text-align: center;
  margin-bottom: 2rem;
}

.register-header h2 {
  color: #333;
  font-size: 1.8rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.register-header p {
  color: #666;
  font-size: 0.9rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-row {
  display: flex;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.form-row .form-group {
  flex: 1;
  margin-bottom: 0;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #555;
  font-weight: 500;
  font-size: 0.9rem;
}

.form-group input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 1rem;
  transition: all 0.3s;
}

.form-group input:focus {
  border-color: #6e8efb;
  box-shadow: 0 0 0 2px rgba(110, 142, 251, 0.1);
  outline: none;
}

.terms-agree {
  margin-bottom: 1.5rem;
}

.checkbox-label {
  display: flex;
  align-items: flex-start;
  cursor: pointer;
  font-size: 0.85rem;
  color: #666;
}

.checkbox-label input {
  margin-right: 0.5rem;
  margin-top: 3px;
}

.checkbox-label a {
  color: #6e8efb;
  text-decoration: none;
  transition: color 0.3s;
}

.checkbox-label a:hover {
  color: #4a6eeb;
  text-decoration: underline;
}

.register-button {
  width: 100%;
  padding: 0.85rem;
  background-color: #6e8efb;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s;
  margin-bottom: 1.5rem;
  display: flex;
  justify-content: center;
  align-items: center;
}

.register-button:hover {
  background-color: #5a77e0;
}

.register-button:disabled {
  background-color: #a0cfff;
  cursor: not-allowed;
}

.loading-spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: #fff;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.login-link {
  text-align: center;
  color: #666;
  font-size: 0.9rem;
}

.login-link a {
  color: #6e8efb;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s;
}

.login-link a:hover {
  color: #4a6eeb;
  text-decoration: underline;
}

/* Font Icons */
[class^="icon-"] {
  font-family: "Font Awesome 5 Free", sans-serif;
  font-weight: 900;
  display: inline-block;
  margin-right: 0.5rem;
}

.icon-user:before {
  content: "\f007";
}

.icon-envelope:before {
  content: "\f0e0";
}

.icon-lock:before {
  content: "\f023";
}

.icon-school:before {
  content: "\f549";
}

.icon-book:before {
  content: "\f02d";
}

/* 响应式调整 */
@media (max-width: 580px) {
  .register-card {
    padding: 1.5rem;
  }
  
  .form-row {
    flex-direction: column;
    gap: 0;
  }
  
  .form-row .form-group {
    margin-bottom: 1.5rem;
  }
  
  .register-header h2 {
    font-size: 1.5rem;
  }
}
</style> 